﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ajax_SubmitMsg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext c = HttpContext.Current;
        string rtn = "fail";

        Model.MsgSendInfo msgIngo = new Model.MsgSendInfo();
        string mobile = ReadRequest.RequestString("mobileNum");
        string usertrendsCode = ReadRequest.RequestHtmlEnCode("ck_thecode");
        int type = ReadRequest.RequestInt("Type");//1,注册，2，修改绑定的手机
        Random rad = new Random();
        int randomCode = rad.Next(1000, 10000);
        string content = "";
        if (type == 1)
        {

            content = "【山西米米科技】您的注册验证码是：" + randomCode + "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        }
        else if (type == 2)
        {
            // content = "您的电话验证码是：" + randomCode + "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！【山西米米信息科技有限公司】";
            content = "【山西米米科技】您的注册验证码是：" + randomCode + "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        }
        Model.MsgSendInfo msgInfo = BLL.MsgSendInfo.GetModelAll("msgType=" + type);
        string ipflag = Request.UserHostAddress;
        int effMinutes = 2;
        if (msgInfo != null)
        {
            effMinutes = (DateTime.Now - msgInfo.addTime).Minutes;
        }
        string trendsCode = "";
        try
        {
            trendsCode = System.Web.HttpContext.Current.Session[NtHelp.regitCode].ToString();
        }
        catch { }
        if ((string.IsNullOrEmpty(trendsCode) || string.IsNullOrEmpty(usertrendsCode) || trendsCode.ToUpper() != usertrendsCode.ToUpper()) && type == 1)
        {
            System.Web.HttpContext.Current.Session[NtHelp.adminlogin_ckcode] = "";
            rtn = "请先输入确认码！";
            Response.Write(rtn);
            Response.End();
        }
        else
        {
            if (effMinutes > 1)
            {
                Model.MsgSendInfo msgInfoFlag = BLL.MsgSendInfo.GetModelAll("1=1 ");
                int effMinutesFlag = (DateTime.Now - msgInfoFlag.addTime).Minutes;
                if (effMinutesFlag > 1)
                {
                    //string sn = SendMsg.Uid;
                    string passowrd = SendMsg.key;

                    string url_send_sms = "https://sms.yunpian.com/v1/sms/send.json";
                    string data_send_sms = "apikey=" + passowrd + "&mobile=" + mobile + "&text=" + content;
                    string returnMsg = SendMsg.HttpPost(url_send_sms, data_send_sms);
                    //string url = "http://utf8.sms.webchinese.cn/?Uid=" + sn + "&Key=" + passowrd + "&smsMob=" + mobile + "&smsText=" + content;

                    //if (returnMsg.Equals("-1"))
                    //{
                    //    rtn = "没有该用户账户";
                    //}
                    //else if (returnMsg.Equals("-2"))
                    //{
                    //    rtn = "接口密钥不正确";
                    //}
                    //else if (returnMsg.Equals("-21"))
                    //{
                    //    rtn = "MD5接口密钥加密不正确";
                    //}
                    //else if (returnMsg.Equals("-3"))
                    //{
                    //    rtn = "短信数量不足";
                    //}
                    //else if (returnMsg.Equals("-11"))
                    //{
                    //    rtn = "该用户被禁用";
                    //}
                    //else if (returnMsg.Equals("-14"))
                    //{
                    //    rtn = "短信内容出现非法字符";
                    //}
                    //else if (returnMsg.Equals("-4"))
                    //{
                    //    rtn = "手机号格式不正确";
                    //}
                    //else if (returnMsg.Equals("-41"))
                    //{
                    //    rtn = "手机号码为空";
                    //}
                    //else if (returnMsg.Equals("-42"))
                    //{
                    //    rtn = "短信内容为空";
                    //}
                    //else if (returnMsg.Equals("-51"))
                    //{
                    //    rtn = "短信签名格式不正确";
                    //}
                    //else if (returnMsg.Equals("-6"))
                    //{
                    //    rtn = "IP限制";
                    //}
                    //else
                    //{
                    msgIngo.msgType = type;//1,注册，2，修改绑定的手机
                    msgIngo.code = randomCode.ToString();
                    msgIngo.mobel = mobile;
                    msgIngo.contents = content;
                    msgIngo.addTime = DateTime.Now;
                    msgIngo.ipaddress = ipflag;
                    BLL.MsgSendInfo.Add(msgIngo);
                    rtn = "returnMsg";

                    //}
                }
                else
                {
                    rtn = "1分钟后请重试！";
                }
            }
            else
            {
                rtn = "两分钟后请重试！";
            }
        }
        Response.Write(rtn);
        Response.End();
    }
    public bool R_mobCheck(string mob)
    {
        if (string.IsNullOrEmpty(mob))
        {
            return false;
        }
        mob = mob.Trim();
        if (mob.Length != 11)
        {
            return false;
        }
        if ("11".Equals(mob.Substring(0, 2)))
        {
            return false;
        }
        if (BLL.CMember.GetIsExitmob(mob))
        {
            return false;
        }
        return true;
    }
}